package org.moda.dsl.source.format;

import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import org.moda.common.Either;

import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * Excel格式解析器
 */
public class CsvFormatter implements Formatter {
    @Override
    public Either<Throwable, List<List<String>>> parse(byte[] rawData) {
        try(InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(rawData))) {
            List<List<String>> rs = CsvUtil.getReader(isr).stream()
                .map(CsvRow::getRawList)
                .toList();

            return Either.right(rs);
        } catch (Exception e) {
            return Either.left(e);
        }
    }
}